package com.drojian;

import android.content.Context;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.concurrent.Callable;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class request extends Thread {
    String string;
    Context context;

    public request(Context context) {
        this.context = context;
    }

    @Override
    public void run() {
        OkHttpClient client = new OkHttpClient();
        Request builder = new Request.Builder()
                .url("https://www.wanandroid.com//hotkey/json")
                .build();
        Response execute = null;
        try {
            execute = client.newCall(builder).execute();
            if (execute.isSuccessful()) {
                string = execute.body().string();
                saveData(string);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }

    private void saveData(String string) {
        try {
            File file = new File(context.getExternalFilesDir(null), "keyword.json");
            FileWriter writer = new FileWriter(file);
            writer.write(string);
            writer.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
